package com.cnit1818.generator;

import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Hashtable;

/**
 * Created with IntelliJ IDEA.
 * User: MaYong
 * Date: 12-11-20
 * Time: 上午9:37
 * To change this template use File | Settings | File Templates.
 */


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class Generater {
    private final String templateDir = "E:/SaiTe/danpin/IbatisGeneration/src";

    private Hashtable<String, String> table = new Hashtable<String, String>();

    //    @Resource(name = "oracleWfjWcsSource")
    @Resource(name = "mysqlSource")
    private DataSource dataSource;
    @Resource
    private CreateEntityFile createEntityFile;
    @Resource
    private CreateXMLFile createXMLFile;

    @Value("#{configProperties.entitySrcDir}")
    private String entitySrcDir;
    @Value("#{configProperties.mapperDir}")
    private String mapperDir;
    @Value("#{configProperties.mapperDir}")
    private String entityDir;
    @Value("#{configProperties.xml}")
    private String xmlDir;
/*
    @Value("#{configProperties.dao}")
    private String daoDir;
    @Value("#{configProperties.daoImpl}")
    private String daoImplDir;
    @Value("#{configProperties.service}")
    private String servicelDir;
    @Value("#{configProperties.serviceImpl}")
*/
    private String serviceImplDir;
    @Value("#{configProperties.tables}")
    private String tableslDir;

    public void create(String tableName) throws SQLException, IOException, TemplateException {
        System.out.println("正在生成 ------- " + tableName + " -------");
//        OracleTable mysqlTable = new OracleTable();
        MysqlTable mysqlTable = new MysqlTable();
        Hashtable<String, String> table = mysqlTable.getMysql();
        createEntityFile.createEntity(entityDir, table, dataSource, tableName);
        createXMLFile.createXML(mapperDir, table, dataSource, tableName);
    }

    /**
     * 生成主方法
     *
     * @throws SQLException
     * @throws IOException
     * @throws TemplateException
     */
    @Test
    public void test() throws SQLException, IOException, TemplateException {
        String tables = tableslDir;
        String[] list = tableslDir.split(",");
        for (int i = 0; i < list.length; i++) {
            create(list[i]);
        }
    }

    public static void main(String[] args) {
        System.out.println(Generater.class.getResource("/").getPath());
        if (1 == 1) {
            return;
        }

        System.out.println("Java运行时环境版本:\n" + System.getProperty("java.version"));

        System.out.println("Java 运行时环境供应商:\n" + System.getProperty("java.vendor"));

        System.out.println("Java 供应商的URL:\n" + System.getProperty("java.vendor.url"));

        System.out.println("Java安装目录:\n" + System.getProperty("java.home"));

        System.out.println("Java 虚拟机规范版本:\n" + System.getProperty("java.vm.specification.version"));

        System.out.println("Java 类格式版本号:\n" + System.getProperty("java.class.version"));

        System.out.println("Java类路径：\n" + System.getProperty("java.class.path"));

        System.out.println("加载库时搜索的路径列表:\n" + System.getProperty("java.library.path"));

        System.out.println("默认的临时文件路径:\n" + System.getProperty("java.io.tmpdir"));

        System.out.println("要使用的 JIT 编译器的名称:\n" + System.getProperty("java.compiler"));

        System.out.println("一个或多个扩展目录的路径:\n" + System.getProperty("java.ext.dirs"));

        System.out.println("操作系统的名称:\n" + System.getProperty("os.name"));

        System.out.println("操作系统的架构:\n" + System.getProperty("os.arch"));

        System.out.println("操作系统的版本:\n" + System.getProperty("os.version"));

        System.out.println("文件分隔符（在 UNIX 系统中是“/”）:\n" + System.getProperty("file.separator"));

        System.out.println("路径分隔符（在 UNIX 系统中是“:”）:\n" + System.getProperty("path.separator"));

        System.out.println("行分隔符（在 UNIX 系统中是“/n”）:\n" + System.getProperty("line.separator"));

        System.out.println("用户的账户名称:\n" + System.getProperty("user.name"));

        System.out.println("用户的主目录:\n" + System.getProperty("user.home"));

        MysqlTable mysqlTable = new MysqlTable();
        System.out.println("用户的主目录:\n" + mysqlTable.getClass().getResource("/"));

    }

    @Test
    public void getAdd() {
        try {
            int k = Integer.valueOf("xxx");
            System.out.println(k);
        } catch (DataAccessException e) {
            System.out.println(e.getMessage());
        }
    }

}
